package top.majuan.study.study.servlet.softwaretype;

import top.majuan.study.study.dao.TbsoftwaretypeDAO;
import top.majuan.study.study.entity.Result;
import top.majuan.study.study.entity.Tbsoftwaretype;
import top.majuan.study.study.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "softwaretypeInsertServlet",urlPatterns = "/softwaretype/insert.action")
public class InsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        try{
            String type_name = req.getParameter("type_name");
            String description = req.getParameter("description");
//            部门名称检测
            Tbsoftwaretype check = TbsoftwaretypeDAO.queryByName(type_name);
            if (check != null) {
                Result<String> result = Result.fail("类型名称已经存在", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            check = new Tbsoftwaretype();
            check.setType_name(type_name);
            check.setDescription(description);
            int i = TbsoftwaretypeDAO.insert(check);
            if (i==1) {
                Result<String> result = Result.success("添加成功", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            Result<String> result = Result.fail("添加失败", null);
            writer.println(JsonUtils.stringify(result));
        }catch (Exception e) {
            e.printStackTrace();
            Result<String> result = Result.fail("添加发生错误", null);
            writer.println(JsonUtils.stringify(result));
        }
    }
}
